package com.laow.dao.impl;

import com.laow.dao.EbookDao;
import com.laow.entity.EbookCategory;
import com.laow.entity.EbookEntry;
import com.laow.entity.Page;
import com.laow.utils.PageUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

@Repository("ebookDaoImpl")
public class EbookDaoImpl extends HibernateDaoSupport implements EbookDao {

    public EbookDaoImpl(){}

    @Autowired
    public EbookDaoImpl(@Qualifier("sessionFactory")SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }


    @Override
    public List<EbookCategory> getCategorys() {
        return this.getHibernateTemplate().find("from EbookCategory ");
    }

    @Override
    public List<EbookEntry> ebookEntrys(int categoryId, int currentPage) {
        return this.getHibernateTemplate().execute(new HibernateCallback<List<EbookEntry>>() {
            @Override
            public List<EbookEntry> doInHibernate(Session session) throws HibernateException, SQLException {
                String hql="from EbookEntry ent where ent.ebookCategorys.id=?";
                if (categoryId==0){   // 等于0则查询所有
                    hql="from EbookEntry ";
                }
                Query query = session.createQuery(hql);
                if (categoryId!=0){
                    query.setParameter(0,categoryId);
                }
                int tote = query.list().size();    // 数据总个数
                Page page= PageUtils.getPage(4,tote,currentPage);

                query.setMaxResults(page.getEveryPage());
                query.setFirstResult(page.getBeginIndex());

                List<EbookEntry> list = (List<EbookEntry>)query.list();
                return list;
            }
        });
    }

    @Override
    public EbookEntry ebookEntryToId(int id) {
        return this.getHibernateTemplate().get(EbookEntry.class,id);
    }

    @Override
    public boolean delEntryData(EbookEntry ebookEntry) {
        try {
            this.getHibernateTemplate().delete(ebookEntry);
            return true;
        }catch (RuntimeException e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean udpEntryData(EbookEntry ebookEntry) {
        try {
            this.getHibernateTemplate().update(ebookEntry);
            return true;
        }catch (RuntimeException e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Object addEntryData(EbookEntry ebookEntry) {
        return this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                session.save(ebookEntry);
                return null;
            }
        });
    }

    @Override
    public int selEbookEntryDataCount(int categoryId) {
        String hql="select count(*) from EbookEntry where ebookCategorys.id=?";
        long count;
        if (categoryId==0){
            hql="select count(*) from EbookEntry";
            count = (long)this.getHibernateTemplate().find(hql).listIterator().next();
            return (int)count;
        }
        count = (long)this.getHibernateTemplate().find(hql,categoryId).listIterator().next();
        return (int)count;
    }
}
